package org.blog.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.blog.beans.Blog;
import org.blog.logic.BlogLogic;

/**
 * Servlet implementation class UserSettings
 */
public class BlogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		action(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		action(request, response);
	}
	
	private void action(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		if("search".equals(request.getParameter("do"))) {
			getServletContext().getRequestDispatcher("/WEB-INF/jsp/allBlogs.jsp").forward(request, response);
		}
		else if("view".equals(request.getParameter("do"))) {
			
			int id = Integer.parseInt(request.getParameter("id"));
			
			Blog blog = BlogLogic.getInstance().getBlog(id);
			
			request.getSession().setAttribute("blog", blog);
			
			getServletContext().getRequestDispatcher("/WEB-INF/jsp/blog.jsp").forward(request, response);

		}
		else
			getServletContext().getRequestDispatcher("/Main").forward(request, response);
		
	}

}
